<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="封面" prop="businessFace">
        <div style="display: flex;align-items: center;">
          <el-image v-if="form.businessFace" :src="imageUrl + form.businessFace" class="img"
                    referrerpolicy="no-referrer"></el-image>
          <el-button type="primary" @click="uploads.show=true" size="mini">选择图片</el-button>
        </div>
      </el-form-item>
      <el-form-item label="创业名称" prop="name">
        <el-input v-model="form.name" placeholder="创业名称" maxlength="30" class="col"></el-input>
      </el-form-item>
<!--      <el-form-item label="标签" prop="label">-->
<!--        <el-input v-model="form.label" placeholder="标签" maxlength="30" class="col"></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="所在区域" prop="value">-->
<!--        <el-cascader class="col"-->
<!--                     placeholder="可输入地区名称进行搜索"-->
<!--                     v-model="form.value"-->
<!--                     :options="options"-->
<!--                     clearable-->
<!--                     :props="{ expandTrigger: 'hover' }"-->
<!--                     filterable-->
<!--                     @change="handleChange"></el-cascader>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="地址" prop="address">{{ form.address }}-->
<!--        <el-button icon="el-icon-location" size="mini" type="primary" @click="showChoosePosition=true">-->
<!--          点击选择位置-->
<!--        </el-button>-->
<!--      </el-form-item>-->
      <el-form-item label="内容" prop="introduce">
        <Tinymce v-model="form.introduce" style="min-height: 300px;"></Tinymce>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:business:save']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog v-if="showChoosePosition" :close-on-click-modal="false" :close-on-press-escape="false"
               :destroy-on-close="true"
               :visible.sync="showChoosePosition" append-to-body title="选择位置" top="5vh"
               width="40%">
      <iframe v-if="showChoosePosition" id="choosePosition" frameborder=0
              src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=ZILBZ-FEDCL-6M3PO-M7GUW-PXP5T-ABFB4&referer=零工超市"
              style="width: 100%;height: 500px">
      </iframe>
      <span slot="footer" class="dialog-footer">
            <el-button class="primary" type="primary" @click="showChoosePosition = false">确 定</el-button>
          </span>
    </el-dialog>
    <el-dialog
        :visible.sync="uploads.show"
        title="上传封面"
        :append-to-body="true"
        destroy-on-close
        :close-on-click-modal="false"
        width="1000px">
      <UploadFile
          v-if="uploads.show"
          :multiple="uploads.multiple"
          @getFile="getFile"
          ref="uploads"
      />
    </el-dialog>
  </div>
</template>
<script>
import {detail, save} from "@/api/entrepreneurs";
import {getForm} from "@/utils/utils";
import UploadFile from "@/components/File/UploadFile.vue";
import Tinymce from "@/components/Tinymce/index.vue";
import {optionsList} from "@/api/business";

export default {
  components: {Tinymce, UploadFile},
  props: {
    id: {
      default: null
    }
  },
  data() {
    return {
      form: {
        id: null,
        name: '',
        businessFace: '',
        label: '',
        address: '',
        latitude: '',
        longitude: '',
        introduce: '',
        areaId: '',
        cityId: '',
        value: [],
      },
      showChoosePosition: false,
      rules: {
        name: [{required: true, message: '请填写名称', trigger: ['blur']}],
        businessFace: [{required: true, message: '请设置商家头像', trigger: ['blur']}],
        label: [{required: true, message: '请填写标签', trigger: ['blur']}],
        address: [{required: true, message: '请填写地址', trigger: ['blur']}],
        introduce: [{required: true, message: '请填内容', trigger: ['blur']}],
        value: [{required: true, message: '请填选择所在区域', trigger: ['blur']}],
      },
      roles: [],
      saveLoading: false,
      formLoading: false,
      uploads: {
        show: false,
        multiple: false,
      },
      imageUrl: '',
      imgUploads: {
        show: false,
        multiple: true,
      },
      value: [],
      options: [{
        value: '',
        label: '',
        children: [{
          value: '',
          label: '',
          children: [{
            value: '',
            label: ''
          }]
        }]
      }],
    }
  },
  mounted() {
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getOptions();
    if (this.id) {
      this.getDetail();
    }
    window.addEventListener('message', (event) => {
      // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
      let loc = event.data;
      if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
        this.form.latitude = loc.latlng.lat;
        this.form.longitude = loc.latlng.lng;
        this.form.address = loc.poiaddress
      }
    }, false);
  },
  methods: {
    handleChange(value) {
      this.form.value = value
      this.form.areaId = value[2]
      this.form.cityId = value[1]
    },
    getOptions() {
      optionsList().then(res => {
        this.options = res
      })
    },
    getDetail() {
      this.formLoading = true
      detail(this.id).then(res => {
        getForm(res, this.form);
        let arr = this.form.businessTime.split("-")
        this.form.businessHoursBegin = arr[0]
        this.form.businessHoursEnd = arr[1]
      }).finally(() => {
        this.formLoading = false;
      })
    },
    getFile(fileList) {
      this.form.businessFace = fileList[0].url
      this.uploads.show = false
    },
    getImage(fileList) {
      this.form.file = fileList
      this.imgUploads.show = false
      let ids = []
      this.form.file.forEach(item => {
        ids.push(item.id)
      })
      this.form.fileId = ids.join(',')
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        this.form.businessTime = this.form.businessHoursBegin + "-" + this.form.businessHoursEnd
        save(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideEditDialog')
    }
  }
}
</script>
<style lang="scss" scoped>

.col {
  width: 450px;
}

.img {
  width: 120px;
  height: 120px;
  margin-right: 10px;
}

.imgList {
  display: flex;
  flex-wrap: wrap;

  .img {
    width: 120px;
    height: 120px;
    margin-right: 10px;
  }
}
</style>
